GBML manual / Maps

map_copy

Makes a new map that is a copy of source: the same keys with the same values, and the same capacity. Changing the copy never changes source, and the other way round. It goes on the right-hand side of the line that makes the variable, like map_create, and it costs a whole second map of memory.

Syntax

map_copy(source) -> map<K,V>

Arguments

NameType
sourcemap

Returns

map<K,V>

Description

Makes a new map that is a copy of source: the same keys with the same values, and room for the same number of pairs. Changing the copy never changes source, and changing source never changes the copy.

var backup = map_copy(source_map);

The copy is the same size

The copy has room for exactly as many pairs as source has room for - its capacity, which is not the same as how many it holds right now. A map made with map_create(64, "", 0) that holds 3 pairs makes a copy that holds the same 3 pairs and has room for 64. The capacity is the original's, so it is a power of two as well, and the copy is always big enough. A map has no order, so the copy makes no promise about the order its pairs come out in.

A copy takes its size from source, so there is no size for you to write. A copy given a smaller size of its own could not hold everything the original holds, so putting a size beside one (var backup: map<string, int, 4> = map_copy(source);, or a /// @capacity tag) is refused when the project is checked.

What it costs

A copy is a second whole map. The Game Boy Advance has no spare memory to hand out while a game runs, so the room for the copy is set aside when the game is built, in full, whether or not it ever fills up. A map with room for 256 pairs makes a copy that takes room for 256 pairs, even if it only holds 3 right now. Two copies take the room of two. A copy in an object's variable keeps its room for as long as the object exists, even when nothing uses it any more.

Making the copy takes time as well. It walks through every pair that source holds and copies each one, so the time grows with how many there are. A few pairs copied when something happens - a button press, a new level - is nothing. Hundreds of pairs copied twice in every frame is a real part of the frame spent copying, and it adds up on a real cartridge. Copy when you need to, not every frame.

Where the room comes from depends on where the variable lives:

Where a copy can go

A copy is a new container, and its size belongs to the variable that holds it - so it goes in the same two places map_create does, and nowhere else:

Not inside another call or a sum, not on a line by itself, not in a later event such as Step, and not over a variable that already exists - that variable already has a size, and a copy of a different size cannot be put in it. To fill a map from another one later in the game, empty it with map_clear and set the pairs back one at a time.

What it can copy

source has to be a variable written on its own, like map_copy(scores), whose size the compiler knows: one made in the same event, or an object variable made in Create. Anything else is refused with a plain message:

Giving it the wrong kind of container is refused too: map_copy copies a map and nothing else.

map_length counts the pairs in the copy or in the original.

Example

var scores = map_create(16, "", 0);
map_set(scores, "ada", 3);
var saved = map_copy(scores);   // holds ada = 3
map_set(saved, "ada", 9);       // changes only the copy: map_get(scores, "ada") is still 3

On the GBA

A copy of the fixed-size hash map - the new one has the same N as source, so it is a power of two as well. A copy is a second whole container, so it costs a second container's worth of memory: as many places as the original has room for (its capacity, not just how many it holds right now), and copying is O(n) in how many items the original holds. The Game Boy Advance has no heap, so the copy's size is fixed when the game is built - it is the original's own declared size, which is why the original has to be a variable whose size is known here (otherwise it is refused: a script's parameter could be any size). Like a creation call it has to be the whole right-hand side of the line that makes a variable, and for an object's variable that line is in its Create event, where every instance of the object gets its own copy of the memory. A copy is always the same size as the original, so a size written beside it is refused.

See also: map_createmap_setmap_getMapsset_copyData containers